using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TomboloneXNAlib
{
    public class TomboloneXNA : Game
    {
        private GraphicsDeviceManager graphics;
        private SpriteBatch sprite;
        private Point dimension;
        private Point sDimension;
        private int spacing;
        private getTavola getter;
        private bool[] tavola;

        private Texture2D notExtracted, extracted;
        private SpriteFont font;

        public delegate bool [] getTavola();

        public TomboloneXNA(Point dimension, getTavola getter)
        {
            graphics = new GraphicsDeviceManager(this);
            this.dimension = dimension;
            graphics.PreferredBackBufferWidth = dimension.X;
            graphics.PreferredBackBufferHeight = dimension.Y;

            spacing = this.dimension.Y / 100;
            sDimension.X = (this.dimension.X - (spacing * 3)) / 10;
            sDimension.Y = (this.dimension.Y - (spacing * 4)) / 9;

            Content.RootDirectory = "Content";

            this.getter = getter;
        }

        protected override void LoadContent()
        {
            Color[] bg = new Color[sDimension.X * sDimension.Y];
            sprite = new SpriteBatch(GraphicsDevice);

            //
            // Genera texture (not)extracted
            //
            extracted = new Texture2D(GraphicsDevice, sDimension.X, sDimension.Y);
            notExtracted = new Texture2D(GraphicsDevice, sDimension.X, sDimension.Y);

            for (int i = 0; i < bg.Length; i++)
                bg[i] = Color.Gray;
            notExtracted.SetData(bg);

            for (int i = 0; i < bg.Length; i++)
                bg[i] = Color.Green;
            extracted.SetData(bg);

            font = Content.Load<SpriteFont>("Font1");

            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            tavola = getter();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            int top, left;
            Vector2 position = new Vector2();

            top = spacing;
            left = spacing;

            GraphicsDevice.Clear(Color.White);
            sprite.Begin();

            for (int i = 0; i < tavola.Length; i++)
            {
                if (i != 0)
                {
                    if (i % 5 == 0)
                        left += spacing;
                    if (i % 10 == 0)
                    {
                        top += this.sDimension.Y;
                        left = spacing;
                    }
                    if (i % 30 == 0)
                        top += spacing;
                }

                position.X = left;
                position.Y = top;

                if (tavola[i])
                    sprite.Draw(extracted, position, null, Color.White, 0, Vector2.Zero, new Vector2(sDimension.X / extracted.Width, sDimension.Y/extracted.Height), SpriteEffects.None, 0);
                else
                    sprite.Draw(notExtracted, position, null, Color.White, 0, Vector2.Zero,new Vector2(sDimension.X / notExtracted.Width, sDimension.Y / notExtracted.Height) , SpriteEffects.None, 0);

                position.X += (this.sDimension.X - font.MeasureString((i + 1).ToString()).X) / 2;
                position.Y += (this.sDimension.Y - font.MeasureString((i + 1).ToString()).Y) / 2;

                sprite.DrawString(font, (i+1).ToString(), position, Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

                left += this.sDimension.X;
            }
            
            sprite.End();
            base.Draw(gameTime);
        }

        private Texture2D getLine(Point dimension, Color colore)
        {
            Texture2D line = new Texture2D(GraphicsDevice, dimension.X, dimension.Y);
            Color[] bg = new Color[dimension.X * dimension.Y];

            for (int i = 0; i < bg.Length; i++)
                bg[i] = colore;
            
            line.SetData(bg);

            return line;
        }

        private Texture2D getLine(int x, int y, Color colore)
        {
            return getLine(new Point(x, y), colore);
        }

    }
}
